package com.his.outpatientvisit.controller;

import com.his.common.verification.group.Insert;
import com.his.config.annotation.CurrentUser;
import com.his.dictresource.service.ResourceControlService;
import com.his.dictresource.vo.ResourceControlVo;
import com.his.finance.service.FinInvoiceStoreService;
import com.his.login.entity.DictEmployee;
import com.his.outpatientvisit.service.*;
import com.his.outpatientvisit.vo.*;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value="/api/patioutvisit")
public class PatiOutVisitController extends ResultBaseController {

    private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @Resource
    private PatiOutVisitService patiOutVisitService;

    @Resource
    private PatiOutVisitSettleItemService patiOutVisitSettleItemService;

    @Resource
    private PatiOutVisitSettleService patiOutVisitSettleService;

    @Resource
    private PatiOutVisitSettlePaylistService patiOutVisitSettlePaylistService;

    @Resource
    private PatiOutVisitInvoiceService patiOutVisitInvoiceService;

    @Resource
    private PatiOutVisitInvoiceLinkService patiOutVisitInvoiceLinkService;

    @Resource
    private FinInvoiceStoreService finInvoiceStoreService;

    @Resource
    private ResourceControlService resourceControlService;

    /**************************************门诊挂号科室查询******************************************/
    @PostMapping(value="/out_org_select.do")
    public ResultUtil out_org_select(@CurrentUser DictEmployee employee){
        try {
            return this.success("科室查询成功",  resourceControlService.selectRosourceControlInfo(employee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**************************************门诊挂号科室下医生当前就诊排队序号查询******************************************/
    @PostMapping(value="/out_serialno_select.do")
    public ResultUtil out_serialno_select(@CurrentUser DictEmployee employee , @RequestBody PatiOutVisitVo patiOutVisitVo){
        try {
            return this.success("排队序号查询成功",  patiOutVisitService.selectPatiOutVisitSerialNo(employee,patiOutVisitVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**************************************门诊挂号科室下医生查询******************************************/
    @PostMapping(value="/out_doc_select.do")
    public ResultUtil out_doc_select(@CurrentUser DictEmployee employee , @RequestBody ResourceControlVo resourceControlVo){
        try {
            return this.success("医生查询成功",  resourceControlService.selectResourceControlDoctor(employee,resourceControlVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }


    /**************************************病人门诊挂号登记表******************************************/

    @PostMapping(value="/outvisit_add.do")
    public ResultUtil outvisit_add(@CurrentUser DictEmployee employee ,@Validated({Insert.class}) @RequestBody PatiOutVisitAllVo patiOutVisitAllVo){
        try {
            //门诊挂号登记表
            String serialNumber = patiOutVisitService.insertPatiOutVisit(employee,patiOutVisitAllVo.getPatiOutVisitVo());
            //门诊挂号结算表
            String settleId = patiOutVisitSettleService.insertPatiOutVisitSettle(employee,patiOutVisitAllVo.getPatiOutVisitSettleVo(),serialNumber);
            //门诊挂号收费项目表
//            patiOutVisitSettleItemService.insertPatiOutVisitSettleItem(patiOutVisitAllVo.getPatiOutVisitSettleItemVo(), settleId);
            return this.success("挂号成功",  settleId );
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/outvisit_update.do")
    public ResultUtil outvisit_update(@CurrentUser DictEmployee employee , @RequestBody PatiOutVisitVo patiOutVisitVo ){
        try {
            patiOutVisitService.updatePatiOutVisit(employee,patiOutVisitVo);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/outvisit_list.do")
    public ResultUtil outvisit_list(@CurrentUser DictEmployee employee , @RequestBody PatiOutVisitVo patiOutVisitVo){
        try {
            return this.success("成功", patiOutVisitService.selectPatiOutVisitList(patiOutVisitVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    /** 
     * 
     * 门诊挂号统计
     * [employee, patiOutVisitVo] 
     * @return com.xiao.common.util.ResultUtil
     * @date 2020/3/27 22:03
     */
    @PostMapping(value="/outvisit_statement.do")
    public ResultUtil outvisit_statement(@CurrentUser DictEmployee employee){
        try {
            return this.success("挂号列表查询成功", patiOutVisitService.selectPatiOutVisitStatementList(employee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 接诊
     * @param employee
     * @param patiOutVisitVo
     * @return
     */
    @PostMapping(value="/receivepati.do")
    public ResultUtil receivePati(@CurrentUser DictEmployee employee , @RequestBody PatiOutVisitVo patiOutVisitVo ){
        try {
            patiOutVisitService.receivepati(employee,patiOutVisitVo);
            return this.success("接诊成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 取消接诊
     * @param employee
     * @param patiOutVisitVo
     * @return
     */
    @PostMapping(value="/receivepati_canc.do")
    public ResultUtil receivepati_canc(@CurrentUser DictEmployee employee , @RequestBody PatiOutVisitVo patiOutVisitVo ){
        try {
            patiOutVisitService.receivepati_canc(employee,patiOutVisitVo);
            return this.success("取消接诊成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    
    /**************************************门诊挂号结算方式******************************************/

    /* 
     * 
     * 新增挂号支付方式 
     * [employee, patiOutVisitAllVo] 
     * @return com.xiao.common.util.ResultUtil
     * @date 2020/3/23 11:48
     */
    @PostMapping(value="/out_paylist_add.do")
    public ResultUtil out_paylist_add(@CurrentUser DictEmployee employee ,
                                      @RequestBody PatiOutVisitAllVo patiOutVisitAllVo,String settleId){
        try {
            //挂号支付方式
            patiOutVisitSettlePaylistService.insertPatiOutVisitSettlePayList(employee,patiOutVisitAllVo.getPatiOutVisitSettlePaylistVo());
            //挂号收据表
            patiOutVisitInvoiceService.insertPatiOutVisitInvoice(employee, patiOutVisitAllVo.getPatiOutVisitInvoiceVo());
            //挂号收据表与挂号结算表关系表
            patiOutVisitInvoiceLinkService.insertPatiOutVisitInvoiceLink(employee,patiOutVisitAllVo.getPatiOutVisitInvoiceLinkVo(),settleId);
            //更新发票总数量
            finInvoiceStoreService.updateFinInvoiceStore(employee,patiOutVisitAllVo.getFinInvoiceStoreVo());
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }
    /* 
     * 
     * 取消挂号支付方式
     * [employee, patiOutVisitAllVo] 
     * @return com.xiao.common.util.ResultUtil
     * @date 2020/3/23 13:47
     */
    @PostMapping(value="/out_paylist_update.do")
    public ResultUtil out_paylist_update(@CurrentUser DictEmployee employee ,
                                      @RequestBody PatiOutVisitSettlePaylistVo patiOutVisitSettlePaylistVo){
        try {
            patiOutVisitSettlePaylistService.updatePatiOutVisitSettlePayList(employee,patiOutVisitSettlePaylistVo);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    
    /* 
     * 
     * 查询挂号支付方式列表
     * [patiOutVisitSettlePaylistVo] 
     * @return com.xiao.common.util.ResultUtil
     * @date 2020/3/23 14:23
     */
    @PostMapping(value="/out_paylist_list.do")
    public ResultUtil out_paylist_list(@RequestBody PatiOutVisitSettlePaylistVo patiOutVisitSettlePaylistVo){
        try {
            return this.success("成功",patiOutVisitSettlePaylistService.selectPatiOutVisitSettlePayList(patiOutVisitSettlePaylistVo.getPatiid(),patiOutVisitSettlePaylistVo.getSettleid()));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /* 
     * 
     * 退号 
     * [employee, patiOutVisitAllVo] 
     * @return com.xiao.common.util.ResultUtil
     * @date 2020/3/20 13:50
     */
    @PostMapping(value="/outinvoice.do")
    public ResultUtil outvisit_update(@CurrentUser DictEmployee employee ,
                                      @RequestBody PatiOutVisitAllVo patiOutVisitAllVo){
        try {
            //1、登记表更新
            patiOutVisitService.updatePatiOutVisit(employee,patiOutVisitAllVo.getPatiOutVisitVo());

            //2、门诊访问结算表插入负记录
            String settleId = patiOutVisitSettleService.insertPatiOutVisitSettleOut(employee,patiOutVisitAllVo.getPatiOutVisitSettleVo());
            //2-1、门诊结算表作废
            patiOutVisitSettleService.updatePatiOutVisitSettle(employee,patiOutVisitAllVo.getPatiOutVisitSettleVo());

            //3、写入结算项目表
//            patiOutVisitSettleItemService.insertPatiOutVisitSettleItemOut(patiOutVisitAllVo.getPatiOutVisitSettleItemVo(),settleId);

            //4、写入支付方式表
            patiOutVisitSettlePaylistService.insertPatiOutVisitSettlePayList(employee,patiOutVisitAllVo.getPatiOutVisitSettlePaylistVo());
            //写入收据表
            patiOutVisitInvoiceService.insertPatiOutVisitInvoice(employee,patiOutVisitAllVo.getPatiOutVisitInvoiceVo());
            //写入收据表与结算表关系表
            patiOutVisitInvoiceLinkService.insertPatiOutVisitInvoiceLink(employee,patiOutVisitAllVo.getPatiOutVisitInvoiceLinkVo(),settleId);
            //更新发票总数量
            finInvoiceStoreService.updateFinInvoiceStore(employee,patiOutVisitAllVo.getFinInvoiceStoreVo());
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

//    @PostMapping(value="/outinvoice_add.do")
//    public ResultUtil out_invoice_add(@CurrentUser DictEmployee employee , @RequestBody PatiOutVisitSettleVo patiOutVisitSettleVo){
//        try {
//            patiOutVisitSettleService.insertPatiOutVisitSettle(employee,patiOutVisitSettleVo);
//            return this.success("成功", null);
//        }catch (Exception e){
//            e.printStackTrace();
//            logger.error(e.getMessage());
//            return this.fail(e.getMessage(),null);
//        }
//    }
}
